package com.example.shangjinlieren.compontents;

import com.example.shangjinlieren.theme.MyColors;
import com.example.shangjinlieren.utils.RpxUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

import java.util.ArrayList;
import java.util.List;

public class Swiper {
    private int id;//标签ID
    private int idSwiperIndicator;//标签ID
    private AbilitySlice abilitySlice;
    private List<Image> components;

    public Swiper(int idSwiper,int idSwiperIndicator, AbilitySlice abilitySlice, List<Image> components) {
        this.id = idSwiper;
        this.idSwiperIndicator = idSwiperIndicator;
        this.abilitySlice = abilitySlice;
        this.components = components;
        init();
    }

    private void init(){

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(MyColors.themeColorGb);
        PageSlider pageSlider =(PageSlider) abilitySlice.findComponentById(id);//翻页
        PageSliderIndicator pageSliderIndicator =(PageSliderIndicator) abilitySlice.findComponentById(idSwiperIndicator);//翻页
//        PageSliderIndicator pageSliderIndicator = new PageSliderIndicator(abilitySlice.getContext());//指示器
//        pageSliderIndicator.setPageSlider(pageSlider);
        pageSliderIndicator.setViewPager(pageSlider);
        pageSliderIndicator.setHeight((int) RpxUtil.rpx(80,abilitySlice));
        pageSliderIndicator.setWidth((int) RpxUtil.rpx(750,abilitySlice));
        //选中效果
        ShapeElement selectedShapeElement = new ShapeElement();
        selectedShapeElement.setRgbColor(MyColors.themeColorGb);
        selectedShapeElement.setCornerRadius((int) RpxUtil.rpx(10,abilitySlice));
        //没选中效果
        ShapeElement normalShapeElement = new ShapeElement();
        normalShapeElement.setCornerRadius((int) RpxUtil.rpx(10,abilitySlice));
        normalShapeElement.setStroke(3,MyColors.themeColorGb);
        normalShapeElement.setRgbColor(MyColors.secondBorderRgb);
        pageSliderIndicator.setItemOffset(20);
        pageSliderIndicator.setPadding(0,0,0,0);
        pageSliderIndicator.setItemElement(normalShapeElement,selectedShapeElement);
//        pageSlider.setBackground(shapeElement);
        pageSlider.setWidth((int) RpxUtil.rpx(750,abilitySlice.getContext()));
        pageSlider.setHeight((int) RpxUtil.rpx(300,abilitySlice.getContext()));
        pageSlider.setProvider(new SwiperProvider(components));
        pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
                //在选择新页面时回调。
                System.out.println("在选择新页面时回调"+i);
            }

            @Override
            public void onPageSlideStateChanged(int i) {
                //当页面幻灯片状态更改时调用。
                System.out.println("更改时调用"+i);
            }

            @Override
            public void onPageChosen(int i) {
//                    当页面滑动时调用。
                System.out.println("页面滑动了"+i);

            }
        });

    }
    public class SwiperProvider extends PageSliderProvider {
        private AbilitySlice slice;
        private List<Image> images=new ArrayList<>();

        public SwiperProvider(List<Image> images) {
            this.images = images;
        }

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int position) {
            Component component = null;
            if (componentContainer == null) {
                return component;
            }
            if (0 <= position && position < images.size()) {
                component = images.get(position);
            }
            componentContainer.addComponent(component);
            return component;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object obj) {
            if (componentContainer == null) {
                return;
            }
            if (obj instanceof Component) {
                componentContainer.removeComponent((Component) obj);
            }
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
            return true;
        }
    }
}

